﻿using Omabar.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Omabar.CMS.Handler
{
    /// <summary>
    /// FileManagerJson 的摘要说明
    /// </summary>
    public class FileManagerJson : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string path = context.Request["path"];
                string order = context.Request["order"];
                string dir = context.Request["dir"];

                string json = string.Empty;
                json += "{\"current_dir_path\":\"" + path + "\",\"current_url\":\"" + AppConst.Domain + "UpLoad/" + path + "\",\"moveup_dir_path\":\"\"";

                if (string.IsNullOrEmpty(path))
                {
                    string[] dirs = System.IO.Directory.GetDirectories(context.Server.MapPath("../UpLoad"), "*", System.IO.SearchOption.TopDirectoryOnly);
                    json += ",\"total_count\":" + dirs.Length;
                    json += ",\"file_list\":[";
                    int i = 0;
                    foreach (string d in dirs)
                    {
                        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(d);
                        if (i > 0)
                            json += ",";
                        json += "{\"is_dir\":true,\"has_file\":true,\"filename\":\"" + di.Name + "\",\"filetype\":\"\",\"filesize\":0,\"datetime\":\"" + di.CreationTime.ToString("yyyy-MM-dd HH:mm:ss") + "\",\"is_photo\":false}";
                        i++;
                    }
                    json += "]}";
                }
                else
                {
                    string[] files = System.IO.Directory.GetFiles(context.Server.MapPath("../UpLoad/" + path));

                    json += ",\"total_count\":" + files.Length;
                    json += ",\"file_list\":[";
                    int i = 0;
                    foreach (string d in files)
                    {
                        System.IO.FileInfo fi = new System.IO.FileInfo(d);
                        if (i > 0)
                            json += ",";
                        json += "{\"is_dir\":false,\"has_file\":false,\"dir_path\":\"\",\"filename\":\"" + fi.Name + "\",\"filetype\":\"" + fi.Extension + "\",\"filesize\":" + fi.Length + ",\"datetime\":\"" + fi.CreationTime.ToString("yyyy-MM-dd HH:mm:ss") + "\",\"is_photo\":true}";
                        i++;
                    }
                    json += "]}";
                }

                context.Response.ContentType = "text/plain";
                context.Response.Write(json);
            }
            catch (Exception ex)
            {
                Tom.Net.Log.LogManager.Error("获取文件夹或文件出错", ex);
                context.Response.Write("出错");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}